<?php 
//模型：用户
//model/class.user.php
//By QINIAO

class qiUser
{
	var $db;
	
	function qiUser($dbhandle)
	{
		$this->db = $dbhandle;
	}
	
	//获取用户列表分页
	function getUsers($page = 1, $prePageNum)
	{
	$start_limit = !empty($page) ? ($page - 1) * $prePageNum : 0;
	$limit = $prePageNum ? "LIMIT $start_limit, $prePageNum" : '';
	$res = $this->db->query("select * from ".DB_PREFIX."users order by id desc $limit");
	$users = array();
	while($row = $this->db->fetch_array($res))
	{	
		$users[] = $row;
	}
		return $users;
	}
	//获取用户数
	function getUserNum($virtue, $setvirtue)
	{
	$where = 'where '.$virtue.'='.$setvirtue.'';
	$res = $this->db->query("SELECT * FROM ".DB_PREFIX."users $where" );
	$UserNum = $this->db->num_rows($res);
	return $UserNum;
	}
	//首页9个最新用户
	function getHomeUsers()
	{
	$res = $this->db->query("select * from ".DB_PREFIX."users order by cTime desc limit 9");
	$users = array();
	while($row = $this->db->fetch_array($res))
	{
	$users[] = $row;
	}
	return $users;
	}
		
	//获取一个用户的信息
	function getOneUser($uid)
	{
	$row = $this->db->once_fetch_array("select * from ".DB_PREFIX."users where id=$uid");
	$userDate = array();
	if($row < 1)
	{
	redirect('index.php', 1, '不好意思，你查看的用户不存在!3秒钟后跳转到首页！'); 
	}
	elseif($row)
	{
	$userDate = array(
	'id' => $row['id'],
	'username' => $row['username'],
	'password' => $row['password'],
	'truename' => $row['truename'],
	'email' => $row['email'],
	'isadmin' => $row['isadmin'],
	'gag' => $row['gag'],
	'cTime' => $row['cTime']
	);
	}
	return $userDate;
	}
	//获取用户名
	function getUserName($uid)
	{
	if($uid > 0)
	{
	$res = $this->db->query("SELECT truename FROM ". DB_PREFIX ."users WHERE id = $uid");
	$row = $this->db->fetch_array($res);
	$userName = htmlspecialchars($row['truename']);
	}else {
	$userName = '匿名';
	}
	return $userName;
	}
		
	//更新用户信息
	function updateUser($userDate,$uid)
	{
	$Item = array();
	foreach($userDate as $key => $date)
	{
	$Item[] = "$key='$date'";
	}
	$upStr = implode(',',$Item);
	$this->db->query("update ".DB_PREFIX."users set $upStr where id=$uid");
	}
	
	//修改名字
	function changeName($truename,$uid)
	{
	$sql = "update ".DB_PREFIX."users set truename = '$truename' where id = $uid";
	$this->db->query($sql);
	}
	//修改用户密码
	function changeUserPwd($oldpwd,$newpwd,$uid)
	{
	$res = $this->db->query("select * from ".DB_PREFIX."users where id = $uid");
	$row = $this->db->fetch_array($res);
	if($row['password'] != $oldpwd)
	{
	redirect('set.php?action=pwd', 1, '原密码输入错误！返回重新输入！');
	}
	else
	{
	$sql = "update ".DB_PREFIX."users set password = '$newpwd' where id = $uid";
	$this->db->query($sql);
	redirect('set.php?action=pwd', 1, '密码修改成功，返回到上一页！');
	}
	}
	
	//添加用户
	function addUser($username,$password)
	{
	$sql = "insert into users (name,password) values('$username','$password')";
	$this->db->query($sql);
	}
	
	//删除用户	
	function deleteUser($uid)
	{
	$this->db->query("update ".DB_PREFIX."mlogs set uid='1' where uid=$uid");
	$this->db->query("delete from ".DB_PREFIX."users where id=$uid");
	}
	
	//判断用户名是否存在
	function isUserExist($username,$uid = '')
	{
	$subSql = $uid ? 'and id!='.$uid : '';
	$query = $this->db->query("select id from users where name=$username $subSql");
	$res = $this->db->num_rows($query);
	if($res > 0)
	{
	return true;
	}else{
	return false;
	}
	}
	
	}

?>